package com.sphd.emanu.activity.pointrecharge;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.market.Points;
import com.sphd.emanu.pojo.market.PointsQueryVo;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.BigDisMailUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 *  充值积分设置
 * @Author Hu
 * @Time 2017/8/3 -- 17:06.
 * @Email 87683202@qq.com
 */
public class PointRechargeActivity extends MVPBaseActivity<PointRechargeContract.View, PointRechargePresenter> implements PointRechargeContract.View {

    @BindView(R.id.btn_save)
    TextView btnSave;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.is_open_topup)
    Switch isOpenTopup;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.true_money)
    MaterialEditText trueMoney;
    @BindView(R.id.give_money)
    MaterialEditText giveMoney;
    @BindView(R.id.btn_cancle)
    Button btnCancle;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    AndroidNextInputs inputs;
    PointsQueryVo pointsQueryVo;
    Intent intent;
    Bundle bundle;
    Points points;
    @BindView(R.id.layout_top_up)
    LinearLayout layoutTopUp;
    @BindView(R.id.tv_first)
    TextView tvFirst;
    @BindView(R.id.second)
    TextView second;
    List<Moudle> moudles;
    boolean isCanDo=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_point_recharge);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        isOpenTopup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    layoutTopUp.setVisibility(View.VISIBLE);
                } else {
                    layoutTopUp.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "充值积分设置");
        intent = getIntent();
        bundle = intent.getExtras();
        pointsQueryVo = (PointsQueryVo) bundle.getSerializable(Constant.POINTS_QUERY_VO);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_MAKETING_4);
        if (!isCanDo){
            btnSave.setVisibility(View.GONE);
        }
        for (Points points : pointsQueryVo.getPointsList()) {
            if (points.getType().equals("1")) {
                this.points = points;
            }
        }
        isOpenTopup.setChecked(points.getEnable());
        if (isOpenTopup.isChecked()) {
            layoutTopUp.setVisibility(View.VISIBLE);
        } else {
            layoutTopUp.setVisibility(View.GONE);
        }
        trueMoney.setText(points.getAmount().toString());
        giveMoney.setText(String.valueOf(points.getPonits()));
        title.setVisibility(View.GONE);
        btnConfirm.setVisibility(View.GONE);
        btnCancle.setVisibility(View.GONE);
        tvFirst.setText("充值");
        second.setText("积分");
        trueMoney.setHint("请输入为用户充值的金额");
        giveMoney.setHint("请输入用户获得的积分数");
        inputs = new AndroidNextInputs();
        inputs.add(trueMoney, StaticScheme.NotBlank().msgOnFail("不能为空"), StaticScheme.Numeric().msgOnFail("请填入整数"));
        inputs.add(giveMoney, StaticScheme.NotBlank().msgOnFail("不能为空"), StaticScheme.Numeric().msgOnFail("请填入整数"));

    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        if (inputs.test()) {
            mPresenter.updateDepositPoints(points.getId(), isOpenTopup.isChecked(), BigDisMailUtils.turn(trueMoney.getText().toString().trim(), Constant.POINT),
                    Integer.valueOf(giveMoney.getText().toString().trim()));
        } else {
            toast("请按提示要求填写");
        }
    }

    @Override
    public void upRes(int res) {
        if (res == 1) {
            toast(R.string.up_success);
            setResult(Constant.REQUEST_CODE);
            finish();
        } else {
            toast(R.string.up_failure);
        }
    }
}
